package com.yueya.business.comment.service;

import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.business.comment.model.Comment;

public class CommentService {

	public Page<Comment> pagination(int pageNumber, int pageSize, Map<String, String> map) {
		String select = "SELECT a.*,(select t.type_name from t_comment_type t where t.test_type_id = a.test_type_id) as test_type_name ";
		StringBuffer sb = new StringBuffer();
		sb.append(" FROM t_comment a ");
		sb.append(" WHERE a.del_flag = '0' ");

		if (StrKit.notBlank(map.get("ref_id"))) {
			sb.append(" AND a.ref_id = '" + map.get("ref_id") + "' ");
		}
		if (StrKit.notBlank(map.get("create_time"))) {
			sb.append(" AND a.create_time = '" + map.get("create_time") + "' ");
		}
		sb.append(" order by a.create_time desc ");
		String sqlExceptSelect = sb.toString();
		return Comment.dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
	}

	public List<Record> getList(String refId) {
		String select = "SELECT a.*,u.user_name ";
		StringBuffer sb = new StringBuffer(select);
		sb.append(" FROM t_comment a left join t_user u on u.user_id = a.user_id ");
		sb.append(" WHERE a.del_flag = '0' AND a.ref_id = ? order by a.create_time desc");

		return Db.find(sb.toString(), refId);
	}

	public boolean remove(String id) {
		String sql = "update t_comment set del_flag = '1' where test_id = ?";
		int num = Db.update(sql, id);

		if (num > 0) {
			return true;
		}
		return false;
	}

	public boolean bathremove(String idsStr) {
		boolean bl = false;
		if (idsStr != null) {
			String[] ids = idsStr.split(";");

			for (int i = 0; i < ids.length; i++) {
				if (!"".equals(ids[i]) && ids[i] != null) {

					if (remove(ids[i])) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}
			}
		}
		return bl;
	}

}
